<?
require_once(dirname(__FILE__).'/../models/Offers.php');

class OffersController extends Main_Controller_Action {
	public function preDispatch()
    {
        if (!unserialize(Zend_Auth::getInstance()->getIdentity())->getIsAdministrator()) {        
                $this->_helper->redirector('', '');
        } else {
           
               /**
			 * Translation - Loading dictionaries common and backoffice_common
			 */
			$commonLocale = Translator::loadTranslation($this->currentLang, 'common');
			$this->view->commonLocale = $commonLocale;
        }
    }
	function indexAction(){
		try{
                $this->view->offers = ModelMapper::instance('Offers')->findAll();
                } catch (DatabaseException $e){
                        $e->getMessage();
		}
		
	}
	function addAction(){

		$smarty = $this->view->getEngine();

		$this->view->categories = ModelMapper::instance('Categories')->findAll();
		$tpl = $smarty->fetch('offers/add.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
	}
	
	function editAction(){

		$offer = ModelMapper::instance('Offers')->find(intval($this->getRequest()->getParam('offersId')));
		
		$smarty = $this->view->getEngine();
		
		$this->view->categories = ModelMapper::instance('Categories')->findAll();
		$this->view->offer = $offer;
		
		$tpl = $smarty->fetch('offers/edit.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
	}
	
	function saveAction(){
		Database::instance()->getConn()->debug =1;
		$formData = $this->_request->getParam('addForm');
		
		if (intval($formData['offersId']) >0)
			$obj = ModelMapper::instance('Offers')->find(intval($formData['offersId']));
		else
			$obj = new Offers();
		

		exit();
	}
	
	function deleteAction(){
		$obj = ModelMapper::instance('Offers')->find(intval($this->getRequest()->getParam('offersId')));
		$obj->delete();
		
		exit();
	}
}

	
?>
